﻿using Hrm.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Autofac;
using Microsoft.AspNet.Identity;
using System.Security.Principal;
using Newtonsoft.Json;

namespace Hrm.Services
{
    public class BaseService<T> : IBaseService<T> where T : Entity
    {
        IRepository<T> _repository;
        IPrincipal _user;
        JsonSerializerSettings jsonSetting;

        public BaseService(IRepository<T> repository)
        {
            _repository = repository;
            _user = HttpContext.Current.User;
            jsonSetting = new JsonSerializerSettings();
            jsonSetting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        }

        public IPrincipal CurrentUser
        {
            get
            {
                return this._user;
            }
        }

        public JsonSerializerSettings JsonSetting
        {
            get
            {
                return this.jsonSetting;
            }
        }

        public T GetById(object id)
        {
            return _repository.GetById(id);
        }

        public T Get(Func<T, bool> predicate)
        {
            return _repository.Get(predicate);
        }

        public IEnumerable<T> GetAll()
        {
            return _repository.GetAll();
        }

        public IEnumerable<T> GetList(Func<T, bool> predicate)
        {
            return _repository.GetList(predicate);
        }

        public IQueryable<T> GetAllAbledAndOrderByCreatetime()
        {
            return _repository.Table.Where(p => p.IsEnable).OrderByDescending(p => p.CreateDatetime);
        }

        public IQueryable<T> GetListAbledAndOrderByCreatetime(Func<T, bool> predicate)
        {
            return _repository.GetList(predicate).AsQueryable().Where(p => p.IsEnable == true).OrderByDescending(p => p.CreateDatetime);
        }

        public int Insert(T entity)
        {
            if (_user != null)
            {
                entity.CreateDatetime = DateTime.Now;
                entity.CreateUserCode = _user.Identity.GetUserId();
                GlobalQueue.LogQueue.Enqueue(new Log()
                {
                    Origin = _user.Identity.Name + "-添加-" + typeof(T).Name,
                    Level = (int)EnumLogLevel.Trace,
                    Message = JsonConvert.SerializeObject(entity, jsonSetting),
                    StackTrade = string.Empty,
                    CreateDatetime = DateTime.Now,
                    CreateUserCode = _user.Identity.GetUserId()
                });
            }
            return _repository.Insert(entity);
        }

        public void Insert(IEnumerable<T> entities)
        {
            if (_user != null)
            {
                foreach (var entity in entities)
                {
                    entity.CreateDatetime = DateTime.Now;
                    entity.CreateUserCode = _user.Identity.GetUserId();
                }
                GlobalQueue.LogQueue.Enqueue(new Log()
                {
                    Origin = _user.Identity.Name + "-添加-" + typeof(T).Name,
                    Level = (int)EnumLogLevel.Trace,
                    Message = JsonConvert.SerializeObject(entities, jsonSetting),
                    StackTrade = string.Empty,
                    CreateDatetime = DateTime.Now,
                    CreateUserCode = _user.Identity.GetUserId()
                });
            }
            _repository.Insert(entities);
        }

        public int Update(T entity)
        {
            if (_user != null)
            {
                entity.UpdateDatetime = DateTime.Now;
                entity.UpdateUserCode = _user.Identity.GetUserId();
                GlobalQueue.LogQueue.Enqueue(new Log()
                {
                    Origin = _user.Identity.Name + "-更新-" + typeof(T).Name,
                    Level = (int)EnumLogLevel.Trace,
                    Message = JsonConvert.SerializeObject(entity, jsonSetting),
                    StackTrade = string.Empty,
                    CreateDatetime = DateTime.Now,
                    CreateUserCode = _user.Identity.GetUserId()
                });
            }
            return _repository.Update(entity);
        }

        public void Update(IEnumerable<T> entities)
        {
            if (_user != null)
            {
                foreach (var entity in entities)
                {
                    entity.UpdateDatetime = DateTime.Now;
                    entity.UpdateUserCode = _user.Identity.GetUserId();
                }
                GlobalQueue.LogQueue.Enqueue(new Log()
                {
                    Origin = _user.Identity.Name + "-更新-" + typeof(T).Name,
                    Level = (int)EnumLogLevel.Trace,
                    Message = JsonConvert.SerializeObject(entities, jsonSetting),
                    StackTrade = string.Empty,
                    CreateDatetime = DateTime.Now,
                    CreateUserCode = _user.Identity.GetUserId()
                });
            }
            _repository.Update(entities);
        }

        public int UpdateDisabled(T entity)
        {
            if (_user != null)
            {
                entity.IsEnable = false;
                entity.UpdateDatetime = DateTime.Now;
                entity.UpdateUserCode = _user.Identity.GetUserId();
                GlobalQueue.LogQueue.Enqueue(new Log()
                {
                    Origin = _user.Identity.Name + "-更新不可用状态-" + typeof(T).Name,
                    Level = (int)EnumLogLevel.Trace,
                    Message = JsonConvert.SerializeObject(entity, jsonSetting),
                    StackTrade = string.Empty,
                    CreateDatetime = DateTime.Now,
                    CreateUserCode = _user.Identity.GetUserId()
                });
            }
            return _repository.Update(entity);
        }

        public void UpdateDisabled(IEnumerable<T> entities)
        {
            if (_user != null)
            {
                foreach (var entity in entities)
                {
                    entity.IsEnable = false;
                    entity.UpdateDatetime = DateTime.Now;
                    entity.UpdateUserCode = _user.Identity.GetUserId();
                }
                GlobalQueue.LogQueue.Enqueue(new Log()
                {
                    Origin = _user.Identity.Name + "-更新不可用状态-" + typeof(T).Name,
                    Level = (int)EnumLogLevel.Trace,
                    Message = JsonConvert.SerializeObject(entities, jsonSetting),
                    StackTrade = string.Empty,
                    CreateDatetime = DateTime.Now,
                    CreateUserCode = _user.Identity.GetUserId()
                });
            }
            _repository.Update(entities);
        }

        public int Delete(object id)
        {
            GlobalQueue.LogQueue.Enqueue(new Log()
            {
                Origin = _user.Identity.Name + "-删除-" + typeof(T).Name,
                Level = (int)EnumLogLevel.Trace,
                Message = JsonConvert.SerializeObject(id, jsonSetting),
                StackTrade = string.Empty,
                CreateDatetime = DateTime.Now,
                CreateUserCode = _user.Identity.GetUserId()
            });
            return _repository.Delete((int)id);
        }

        public int Delete(T entity)
        {
            GlobalQueue.LogQueue.Enqueue(new Log()
            {
                Origin = _user.Identity.Name + "-删除-" + typeof(T).Name,
                Level = (int)EnumLogLevel.Trace,
                Message = JsonConvert.SerializeObject(entity, jsonSetting),
                StackTrade = string.Empty,
                CreateDatetime = DateTime.Now,
                CreateUserCode = _user.Identity.GetUserId()
            });
            return _repository.Delete(entity);
        }

        public void Delete(IEnumerable<T> entities)
        {
            GlobalQueue.LogQueue.Enqueue(new Log()
            {
                Origin = _user.Identity.Name + "-删除-" + typeof(T).Name,
                Level = (int)EnumLogLevel.Trace,
                Message = JsonConvert.SerializeObject(entities, jsonSetting),
                StackTrade = string.Empty,
                CreateDatetime = DateTime.Now,
                CreateUserCode = _user.Identity.GetUserId()
            });
            _repository.Delete(entities);
        }
    }
}
